package com.hqd.utils.pack;

import com.hqd.utils.file.SimpleFileUtils;
import com.hqd.utils.pack.bean.ProjectInfo;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

public class SimplePackUtils {
    public static final String FILE_SUFFIX_JAVA = ".java";
    public static final String FILE_SUFFIX_CLASS = ".class";
    public static final String FILE_SUFFIX_XML = ".xml";
    public static final String FILE_SUFFIX_PROPERTIES = ".properties";

    /**
     * 打包项目文件
     *
     * @param projectInfo 项目信息
     * @param changeFiles 修改的文件
     * @throws IOException
     */
    public static String packageProjectFile(ProjectInfo projectInfo, List<File> changeFiles) throws IOException {
        String cmdPath = getResource();
        StringBuilder sb = new StringBuilder("cmd /c start /wait ")
                .append(cmdPath).append("com/hqd/utils/pack/package.bat ")
                .append(projectInfo.getOutPath()).append(" ")
                .append(projectInfo.getJarPath()).append(" ");
        String srcPath = new File(projectInfo.getBasePath(), projectInfo.getSrcPath()).getAbsolutePath();
        String resourcePath = new File(projectInfo.getBasePath(), projectInfo.getResourcePath()).getAbsolutePath();
        String testSrcPath = new File(projectInfo.getBasePath(), projectInfo.getTestSrcPath()).getAbsolutePath();
        String testResourcePath = new File(projectInfo.getBasePath(), projectInfo.getTestResourcePath()).getAbsolutePath();
        for (File file : changeFiles) {
            String path = file.getAbsolutePath();
            if (projectInfo.isSkipTest()) {
                if (path.startsWith(testResourcePath) || path.startsWith(testSrcPath)) {
                    continue;
                }
            }
            path = path.replace(srcPath, "")
                    .replace(resourcePath.replace(srcPath, ""), "")
                    .replace(testSrcPath, "")
                    .replace(testResourcePath, "")
                    .replace(FILE_SUFFIX_JAVA, FILE_SUFFIX_CLASS);
            if (path.startsWith(File.separator)) {
                sb.append(path.substring(1));
            } else {
                sb.append(path);
            }
            sb.append(" ");
        }
        String cmd = sb.toString();
        runCMD(cmd);
        return cmd;
    }

    private static void runCMD(String cmd) throws IOException {
        try {
            Runtime.getRuntime().exec(cmd).waitFor();
        } catch (InterruptedException e) {
        }
    }

    /**
     * 获取项目路径
     *
     * @return
     */
    private static String getResource() {
        String path = new File(SimplePackUtils.class.getClassLoader().getResource("").getFile()).getAbsolutePath();
        if (StringUtils.isNotBlank(path)) {
            path += File.separator;
        }
        return URLDecoder.decode(path);
    }

    /**
     * 获取修改的文件
     *
     * @param file    目标文件
     * @param day     间隔天数
     * @param suffixs 匹配后缀
     * @return
     */
    public static List<File> getChangeFiles(File file, int day, String... suffixs) {
        return SimpleFileUtils.getFilesInTime(file, new Date(), day, suffixs);
    }
}
